package team.away.android.fubway;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

public class Person {
	private Engine mEngine;
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TextureRegion mFaceTextureRegion;
	private Sprite mSprite;
	private Scene mScene;
	
	final int X_MAX = 680;
	final int X_MIN = 23;
	final int Y_MAX = 420;
	final int Y_MIN = 129;
	
	public void Init(Engine engine, Context context, final int x, final int y){
		mEngine = engine;
		mBitmapTextureAtlas = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");
		mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, context, "player.png", 0, 0);

		this.mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);

		mSprite = new Sprite(x, y, mFaceTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				float pX = pSceneTouchEvent.getX()- this.getWidth() / 2;
				float pY = pSceneTouchEvent.getY() - this.getHeight() / 2;
				if( pX>=X_MIN && pX <= X_MAX-this.getWidth()  && pY>=Y_MIN && pY<=Y_MAX-this.getWidth() ){
					this.setPosition( pX , pY);
				}
				return true;
			}
		};
		mSprite.setScale((float) (80.0/64.0));
	}
	public void BindScene(Scene scene){
		mScene = scene;
		mScene.attachChild(mSprite);
		mScene.registerTouchArea(mSprite);
		mScene.setTouchAreaBindingEnabled(true);
		
	}

}
